package com.fishery.processing.controller;


import com.fishery.entity.Result;
import com.fishery.processing.entity.Job;
import com.fishery.processing.service.JobService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author run
 * @since 2021-03-12
 */
@RestController
@RequestMapping("/processing/job")
@CrossOrigin
@Api(tags = "加工作业相关接口")
public class JobController {

    @Autowired
    private JobService jobService;

    @GetMapping("/{id}/{page}/{size}")
    @ApiOperation("根据加工厂id分页获取加工作业列表")
    public Result getJobsOfFactory(@PathVariable(value = "id") String factoryId
                                    , @PathVariable(value = "page") Integer page
                                    , @PathVariable(value = "size") Integer size){
        return Result.success().data(jobService.getJobsOfFactory(factoryId, page, size));
    }

    @PostMapping("/")
    @ApiOperation("新增加工作业")
    public Result createJob(@RequestBody @ApiParam(value = "job")Job job){
        return jobService.createJob(job)
                ? Result.success("新增成功") : Result.error("新增失败");
    }

    @PutMapping("/")
    @ApiOperation("修改加工作业（已完成的只能修改描述）")
    public Result updateJob(@RequestBody @ApiParam(value = "job") Job job){
        return jobService.updateJob(job)
                ? Result.success("修改成功") : Result.error("修改失败");
    }

    @DeleteMapping("/{id}")
    @ApiOperation("根据作业id删除加工作业")
    public Result deleteJob(@PathVariable(value = "id") String jobId){
        return jobService.deleteJob(jobId)
                ? Result.success("删除成功") : Result.error("删除失败");
    }

    @PutMapping("/{id}")
    @ApiOperation("根据作业id将加工作业状态修改为已完成")
    public Result updateStatus(@PathVariable(value = "id") String jobId){
        return jobService.updateStatus(jobId)
                ? Result.success("修改成功") : Result.error("修改失败");
    }

    @GetMapping("/{id}")
    @ApiOperation("根据作业id获取作业信息")
    public Result getJobById(@PathVariable(value = "id") String jobId){
        return Result.success().data(jobService.getJobById(jobId));
    }

}

